<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //
    protected $table='category';
    protected $primaryKey='cate_id';
    public $timestamps=false;
    protected $guarded=[];
 /*    protected $fillable=['cate_id','cate_order',
        'cate_order','cate_name','cate_title',
        'cate_view','cate_pid','cate_description']; */
    
    
    public function get_class_set($cate_id,&$class_set,$all_class){
        foreach ($all_class as $class){
            if($class['cate_pid']==$cate_id){
                array_push($class_set, $class['cate_id']);
                $this->get_class_set($class['cate_id'], $class_set, $all_class);
            }
        }
        return;
    }
    public function tree(){
        $all_class=Category::orderBy('cate_order')->get();
        $class_tree=array();
        $this->get_class_tree(0, $all_class, $class_tree, '┠');
        return $class_tree;
    }
    public function get_class_tree($id,$all_class,&$class_tree,$seperator){
        $seperator .="━";
        foreach ($all_class as $class){
            if($class['cate_pid']==$id){
                $new_node=array('cate_id'=>$class['cate_id'],
                    'cate_name'=>$class['cate_name'],
                    'cate_title'=>$class['cate_title'],
                    'cate_view'=>$class['cate_view'],
                    'cate_pid'=>$class['cate_pid'],
                    'cate_order'=>$class['cate_order'],
                    'seperator'=>$seperator
                );
                array_push($class_tree, $new_node);
                $this->get_class_tree($class['cate_id'],
                    $all_class, $class_tree, $seperator);
            }
        }
        return;
    }
}
